﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.ExpFormat {
    /// <summary>
    /// Correspond to the Vn node Factor in the grammar(Exp).
    /// </summary>
    internal abstract partial class VnFactor : IVnNode {
        // [6]=Factor : '(' Exp ')' ;
        // [7]=Factor : 'number' ;


        public class C0 : VnFactor {
            // [6]=Factor : '(' Exp ')' ;

            public C0(Token r2, VnExp r1, Token r0) {
                this.r2 = r2;
                this.r1 = r1; r1.Parent = this;
                this.r0 = r0;
                this._scope = new TokenRange(r2, r0);
            }
            private readonly Token r2;
            private readonly VnExp r1;
            private readonly Token r0;

            private readonly TokenRange _scope;
            public override TokenRange Scope => _scope;

            public override void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(this.r2, preConfig, writer);
                // ( Exp )之间不留空格
                var config = new BlankConfig(inlineBlank: 0, forceNewline: false);
                context.PrintCommentsBetween(this.r2, this.r1, config, writer);
                context.PrintBlanksAnd(this.r1, config, writer);
                context.PrintCommentsBetween(this.r1, this.r0, config, writer);
                context.PrintBlanksAnd(this.r0, config, writer);
            }

            //public override IInherit? Parent { get; set; }
            public override IEnumerable<IInherit> ReversedChildren() {
                {
                    var wrap = new IInherit.TokenWrap(this.r0);
                    wrap.Parent = this;
                    yield return wrap;
                }
                {
                    yield return this.r1;
                }
                {
                    var wrap = new IInherit.TokenWrap(this.r2);
                    wrap.Parent = this;
                    yield return wrap;
                }
            }

        }
        public class C1 : VnFactor {
            // [7]=Factor : 'number' ;

            public C1(Token r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
            private readonly Token r0;

            private readonly TokenRange _scope;
            public override TokenRange Scope => _scope;

            public override void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
                // 根据上级设置的preConfig输出自己唯一的token
                context.PrintBlanksAnd(this.r0, preConfig, writer);
            }
            //public override IInherit? Parent { get; set; }
            public override IEnumerable<IInherit> ReversedChildren() {
                var wrap = new IInherit.TokenWrap(this.r0);
                wrap.Parent = this;
                yield return wrap;
            }
        }

        public abstract TokenRange Scope { get; }

        public abstract void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context);

        public IInherit? Parent { get; set; }
        public abstract IEnumerable<IInherit> ReversedChildren();
        public void PrintSelf(TextWriter writer, IReadOnlyList<string>? stArray) {
            writer.Write($"{nameof(VnFactor)}⛪T[{this.Scope}]");
        }
        public override string ToString() {
            return $"{nameof(VnFactor)}⛪T[{this.Scope}]";
        }
    }
}
